export default class Image{
  constructor(path,quad){
    this.w = 0
    this.h = 0
    this.ax = 0
    this.ay = 0
    
    if (typeof(path) == "string"){
      this.texture = wx.createImage()
      this.texture.src = path
      this.texture.onload = this.onLoad.bind(this)
      this.setQuad(0,0,0,0)
    } else {
      this.copyData(path,quad)
    }
  }
  copyData(instant,quad){
    this.texture = instant.texture
    this.w = instant.w
    this.h = instant.h
    this.ax = instant.ax
    this.ay = instant.ay
    this.quad = quad || [...instant.quad]
    this.anchorCenter = instant.anchorCenter
  }
  extend(quad){
    return new Image(this,quad)
  }
  getWidth(){
    return this.w
  }
  getHeight(){
    return this.h
  }
  getDimentions(){
    return { w: this.texture.width, h: this.texture.height}
  }
  onLoad(){
    this.w = this.texture.width
    this.h = this.texture.height
    if (this.quad[3] == 0){
      this.setQuad(0,0,this.w,this.h)
    }
    if (this.anchorCenter) {
      this.setAnchorCenter()
    }
  }
  setQuad(x,y,w,h){
    this.quad = [x, y, w, h]
    if (this.anchorCenter) {
      this.setAnchorCenter()
    }
  }
  setAnchorCenter(){
    this.anchorCenter = true
    this.ax = this.quad[2]/2
    this.ay = this.quad[3]/2
  }
  setAnchor(ax,ay){
    this.ax = ay
    this.ay = ay
  }
  render(ctx){
    ctx.drawImage(this.texture,...this.quad,-this.ax,-this.ay,this.quad[2],this.quad[3])
    //console.log(this.texture, ...this.quad, -this.ax, -this.ay, this.quad[2], this.quad[3])
  }
}